package com.home.model.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import com.home.model.dao.CityDao;
import com.home.model.dao.DistrictDao;
import com.home.model.dto.CityDto;
import com.home.model.dto.DistrictDto;
import com.home.model.entity.City;
import com.home.model.entity.District;
import com.home.model.service.CityService;

@Stateless
public class CityServiceImpl implements CityService{
	
	@EJB
	private CityDao cityDao;
	@EJB
	private DistrictDao districtDao;
	
	/**
	 * find cities include list districts
	 */
	public List<CityDto> findAllCities() {
		List<CityDto> citiesDto = new ArrayList<CityDto>();
		List<City> cities = cityDao.findAllCities();
		
		for (City city : cities) {
			CityDto cityDto = new CityDto(city);
			List<District> districts = city.getDistricts();
			List<DistrictDto> districtsDto = new ArrayList<DistrictDto>();
			for (District district : districts) {
				districtsDto.add(new DistrictDto(district));
			}
			cityDto.setDistricts(districtsDto);
			citiesDto.add(new CityDto(city));
		}
		return citiesDto;
	}
	
}
